package com.java.basis.collection;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;

/**
 * ArrayList的使用
 * 存储结构：数组，查询遍历速度快，增删速度慢
 *
 * @author wangning
 * @create 2021-04-08 22:15
 */
public class TestArrayList01 {
	public static void main(String[] args) {
		//创建集合 nǐ hǎo
		ArrayList<Student> arrayList = new ArrayList<>();
		//1.添加元素
		Student s1 = new Student("刘德华", 20);
		Student s2 = new Student("郭富城", 22);
		Student s3 = new Student("梁朝伟", 18);

		arrayList.add(s1);
		arrayList.add(s2);
		arrayList.add(s3);
		System.out.println("arrayList = " + arrayList.size());
		System.out.println("arrayList = " + arrayList.toString());
		System.out.println("-----------------");
		System.out.println(arrayList.get(0));
		System.out.println(arrayList.get(0));
		//2.删除元素
//		arrayList.remove(s1);
//		arrayList.remove(0);
//		arrayList.remove(new Student("刘德华", 20));//这样是删不掉的
		//如果就像用以上的这种方式删除，需要怎么做？由于对象类型的equals方法只是比较的地址
		//所以我们可以进行重写equals方法来删除该元素
		System.out.println("arrayList = " + arrayList.size());
		System.out.println("arrayList = " + arrayList.toString());
		//3.遍历元素[重点]
		//3.1使用迭代器
		Iterator<Student> iterator = arrayList.iterator();
		while(iterator.hasNext()) {
			System.out.println(iterator.next());
//			arrayList.add(s1);
		}
		//3.2列表迭代器
		System.out.println("----------3.2列表迭代器---------");
		ListIterator<Student> studentListIterator = arrayList.listIterator();
		while (studentListIterator.hasNext()) {
			System.out.println(studentListIterator.next());
		}
		//4.比较
		System.out.println(arrayList.isEmpty());
		System.out.println(arrayList.contains(new Student("刘德华", 20)));

	}
}
